package com.bgs.microservices.service.sms.email.service.impl;

import com.bgs.microservices.service.sms.email.service.EmailService;
import com.bgs.microservices.service.sms.response.CommonResult;
import com.bgs.microservices.service.sms.util.constants.CommonConstants;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;

/**
 * 邮件发送工具类
 * host: smtp.qq.com #邮箱的发送邮件服务器
 * username:  填写自己的邮箱服务器 #开通邮箱服务的邮箱
 * password: 填写授权后的授权码 #授权码
 * default-encoding: UTF-8
 * from: 填写自己的邮箱服务器 #发送者
 * @auther zcy
 * @date 2022/8/11 14:16
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javamailsender;

    @Value("${spring.mail.from}")
    private String fromEmail;


    /**
     * @param text 发送邮件内容
     * @param subject 主题
     * @param to 发给谁
     * @param from 发送者
     */
    public CommonResult sendEmail(String text, String subject, String to, String from){
        SimpleMailMessage message=new SimpleMailMessage();
        // 设置发送者如果没有则认为是服务器发送
        if(!StringUtils.isEmpty(from)){
            fromEmail = from;
        }
        //发给谁
        message.setTo(to);
        //发送者
        message.setFrom(fromEmail);
        //邮件内容
        message.setText(text);
        //主题
        message.setSubject(subject);
        // 发送
        javamailsender.send(message);
        return CommonResult.ok(CommonConstants.SEND_OK);

    }


    /**
     * 发送带有样式的内容
     * @param context 发送邮件内容 包含html
     * @param subject 主题
     * @param to 发给谁
     * @param from 发送者
     */
    public CommonResult sendEmailAndHtml(String context,String subject,String to,String from) throws MessagingException {
       // String context = "<p style=\"background-color: red;color: white\">我是谁</p>";
        MimeMessage mimeMessage = javamailsender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        // 设置发送者如果没有则认为是服务器发送
        if(!StringUtils.isEmpty(from)){
            fromEmail = from;
        }
        //发给谁
        mimeMessageHelper.setTo(to);
        //发送者
        mimeMessageHelper.setFrom(fromEmail);
        //邮件内容 开启html发送
        mimeMessageHelper.setText(context,true);
        //主题
        mimeMessageHelper.setSubject(subject);
        //发送
        javamailsender.send(mimeMessage);
        return CommonResult.ok(CommonConstants.SEND_OK);
    }


    /**
     * 发送带有文件（包含普通文件压缩包图片等）的邮件
     * @param text 发送邮件内容
     * @param subject 主题
     * @param to 发给谁
     * @param from 发送者
     * @throws MessagingException
     */
    public CommonResult sendEmailAndFile(String text,String subject,String to,String from,String filepath) throws MessagingException {
        MimeMessage mimeMessage = javamailsender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = null;
        mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        // 设置发送者如果没有则认为是服务器发送
        if(!StringUtils.isEmpty(from)){
            fromEmail = from;
        }
        //发给谁
        mimeMessageHelper.setTo(to);
        //发送者
        mimeMessageHelper.setFrom(fromEmail);
        //邮件内容 开启html发送
        mimeMessageHelper.setText(text);
        //主题
        mimeMessageHelper.setSubject(subject);
        //添加附件
        FileSystemResource filesystemresource = new FileSystemResource(new File(filepath));
        String filename = filesystemresource.getFilename();
        mimeMessageHelper.addAttachment(filename, filesystemresource);
        //发送
        javamailsender.send(mimeMessage);
        return CommonResult.ok(CommonConstants.SEND_OK);
    }
}
